package com.alex.goods.filter;

import javax.servlet.*;
import javax.servlet.annotation.WebFilter;
import javax.servlet.http.HttpFilter;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

//@WebFilter("/*")
@WebFilter("/*")
public class LoginFilter extends HttpFilter implements Filter {
	private static final long serialVersionUID = 1L;
	public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)
			throws IOException, ServletException {
		HttpServletRequest req = (HttpServletRequest) request;
		String uri = req.getRequestURI();
		Boolean islogin = req.getSession().getAttribute("currentname") != null;		
		if (islogin ||uri.equals("/login") || uri.startsWith("/lib/") || uri.startsWith("/js/") || uri.startsWith("/css/")
				|| uri.startsWith("/images/") || islogin||uri.startsWith("/app.ico") ||
		    uri.startsWith("/reg") || uri.startsWith("/WEB-INF/login") || uri.startsWith("/pageAdminUtil/") ||
				uri.startsWith("/img/")
		) {
			chain.doFilter(request, response);
		} else {
			HttpServletResponse resp = (HttpServletResponse) response;
			resp.sendRedirect("/login");
		}
	}
}
